home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / oleo-1_4.lha / oleo-1.4 / parse.y < prev    next >
Text File  |  1993-01-05  |  18KB  |  879 lines

  1. %{
  2. /*    Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of Oleo, the GNU Spreadsheet.
  5.  
  6. Oleo is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Oleo is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Oleo; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19. %}
  20.  
  21.  
  22. %right '?' ':'
  23. /* %left '|' */
  24. %left '&'
  25. %nonassoc '=' NE
  26. %nonassoc '<' LE '>' GE
  27. %left '+' '-'
  28. %left '*' '/' '%'
  29. %right '^'
  30. %left NEG '!'
  31.  
  32. %token    L_CELL L_RANGE
  33. %token    L_VAR
  34.  
  35. %token    L_CONST
  36. %token    L_FN0    L_FN1    L_FN2    L_FN3    L_FN4    L_FNN
  37. %token    L_FN1R    L_FN2R    L_FN3R    L_FN4R    L_FNNR
  38.  
  39. %token    L_LE    L_NE    L_GE
  40.  
  41. %{
  42. #include "funcdef.h"
  43.  
  44. #include <ctype.h>
  45.  
  46. #define obstack_chunk_alloc ck_malloc
  47. #define obstack_chunk_free free
  48. #include "obstack.h"
  49. #include "sysdef.h"
  50.  
  51. #include "global.h"
  52. #include "errors.h"
  53. #include "node.h"
  54. #include "eval.h"
  55. #include "ref.h"
  56.  
  57. int yylex ();
  58. #ifdef __STDC__
  59. void yyerror (char *);
  60. #else
  61. void yyerror ();
  62. #endif
  63. VOIDSTAR parse_hash;
  64. extern VOIDSTAR hash_find();
  65.  
  66. /* This table contains a list of the infix single-char functions */
  67. unsigned char fnin[] = {
  68.     SUM, DIFF, DIV, PROD, MOD, /* AND, OR, */ POW, EQUAL, IF, CONCAT, 0
  69. };
  70.  
  71. #define YYSTYPE _y_y_s_t_y_p_e
  72. typedef struct node *YYSTYPE;
  73. YYSTYPE parse_return;
  74. #ifdef __STDC__
  75. YYSTYPE make_list (YYSTYPE, YYSTYPE);
  76. #else
  77. YYSTYPE make_list ();
  78. #endif
  79.  
  80. char *instr;
  81. int parse_error = 0;
  82. extern struct obstack tmp_mem;
  83.  
  84. %}
  85. %%
  86. line:    exp
  87.         { parse_return=$1; }
  88.     | error {
  89.         if(!parse_error)
  90.             parse_error=PARSE_ERR;
  91.         parse_return=0; }
  92.     ;
  93.  
  94. exp:      L_CONST
  95.     | cell
  96.     | L_FN0 '(' ')' {
  97.         $$=$1; }
  98.     | L_FN1 '(' exp ')' {
  99.         ($1)->n_x.v_subs[0]=$3;
  100.         ($1)->n_x.v_subs[1]=(struct node *)0;
  101.         $$=$1; }
  102.     | L_FN2 '(' exp ',' exp ')' {
  103.         ($1)->n_x.v_subs[0]=$3;
  104.         ($1)->n_x.v_subs[1]=$5;
  105.         $$=$1; }
  106.     | L_FN3 '(' exp ',' exp ',' exp ')' {
  107.         ($1)->n_x.v_subs[0]=make_list($3,$5);
  108.          ($1)->n_x.v_subs[1]=$7;
  109.          $$=$1;}
  110.     | L_FN4 '(' exp ',' exp ',' exp ',' exp ')' {
  111.         ($1)->n_x.v_subs[0]=make_list($3,$5);
  112.          ($1)->n_x.v_subs[1]=make_list($7,$9);
  113.          $$=$1;}
  114.     | L_FNN '(' exp_list ')' {
  115.         ($1)->n_x.v_subs[0]=(struct node *)0;
  116.         ($1)->n_x.v_subs[1]=$3;
  117.         $$=$1; }
  118.     | L_FN1R '(' L_RANGE ')' {
  119.         $1->n_x.v_subs[0]=$3;
  120.         $$=$1; }
  121.     | L_FN1R '(' L_VAR ')' {
  122.         $1->n_x.v_subs[0]=$3;
  123.         $$=$1; }
  124.  
  125.     | L_FN2R '(' L_RANGE ',' exp ')' {
  126.         $1->n_x.v_subs[0]=$3;
  127.         $1->n_x.v_subs[1]=$5;
  128.         $$=$1; }
  129.     | L_FN2R '(' L_VAR ',' exp ')' {
  130.         $1->n_x.v_subs[0]=$3;
  131.         $1->n_x.v_subs[1]=$5;
  132.         $$=$1; }
  133.  
  134.     /* JF:  These should be FN2R, but I'm hacking this for SYLNK */
  135.     | L_FN2R '(' L_RANGE ',' exp ',' exp ')' {
  136.         if($1->comp_value!=F_INDEX)
  137.             parse_error=PARSE_ERR;
  138.         $1->comp_value=F_INDEX2;
  139.         $1->n_x.v_subs[0]=make_list($3,$5);
  140.         $1->n_x.v_subs[1]=$7;
  141.         $$=$1; }
  142.     | L_FN2R '(' L_VAR ',' exp ',' exp ')' {
  143.         if($1->comp_value!=F_INDEX)
  144.             parse_error=PARSE_ERR;
  145.         $1->comp_value=F_INDEX2;
  146.         $1->n_x.v_subs[0]=make_list($3,$5);
  147.         $1->n_x.v_subs[1]=$7;
  148.         $$=$1; }
  149.  
  150.     | L_FN3R '(' L_RANGE ',' exp ',' exp ')' {
  151.         ($1)->n_x.v_subs[0]=make_list($3,$5);
  152.          ($1)->n_x.v_subs[1]=$7;
  153.          $$=$1;}
  154.     | L_FN3R '(' L_VAR ',' exp ',' exp ')' {
  155.         ($1)->n_x.v_subs[0]=make_list($3,$5);
  156.          ($1)->n_x.v_subs[1]=$7;
  157.          $$=$1;}
  158.  
  159.     | L_FNNR '(' range_exp_list ')' {
  160.         ($1)->n_x.v_subs[0]=(struct node *)0;
  161.         ($1)->n_x.v_subs[1]=$3;
  162.         $$=$1; }
  163.     | exp '?' exp ':' exp {
  164.         $2->comp_value=IF;
  165.         $2->n_x.v_subs[0]=$4;
  166.         $2->n_x.v_subs[1]=$5;
  167.         $4->n_x.v_subs[0]=$1;
  168.         $4->n_x.v_subs[1]=$3;
  169.         $$=$2; }
  170.     /* | exp '|' exp {
  171.         $2->n_x.v_subs[0]=$1;
  172.         $2->n_x.v_subs[1]=$3;
  173.         $$ = $2; } */
  174.     | exp '&' exp {
  175.         $2->n_x.v_subs[0]=$1;
  176.         $2->n_x.v_subs[1]=$3;
  177.         $$ = $2; }
  178.     | exp '<' exp {
  179.         $2->n_x.v_subs[0]=$1;
  180.         $2->n_x.v_subs[1]=$3;
  181.         $$ = $2; }
  182.     | exp LE exp {
  183.         $2->n_x.v_subs[0]=$1;
  184.         $2->n_x.v_subs[1]=$3;
  185.         $$ = $2; }
  186.     | exp '=' exp {
  187.         $2->n_x.v_subs[0]=$1;
  188.         $2->n_x.v_subs[1]=$3;
  189.         $$ = $2; }
  190.     | exp NE exp {
  191.         $2->n_x.v_subs[0]=$1;
  192.         $2->n_x.v_subs[1]=$3;
  193.         $$ = $2; }
  194.     | exp '>' exp {
  195.         $2->n_x.v_subs[0]=$1;
  196.         $2->n_x.v_subs[1]=$3;
  197.         $$ = $2; }
  198.     | exp GE exp {
  199.         $2->n_x.v_subs[0]=$1;
  200.         $2->n_x.v_subs[1]=$3;
  201.         $$ = $2; }
  202.     | exp '+' exp {
  203.         $2->n_x.v_subs[0]=$1;
  204.         $2->n_x.v_subs[1]=$3;
  205.         $$ = $2; }
  206.     | exp '-' exp {
  207.         $2->n_x.v_subs[0]=$1;
  208.         $2->n_x.v_subs[1]=$3;
  209.         $$ = $2; }
  210.     | exp '*' exp {
  211.         $2->n_x.v_subs[0]=$1;
  212.         $2->n_x.v_subs[1]=$3;
  213.         $$ = $2; }
  214.     | exp '/' exp {
  215.         $2->n_x.v_subs[0]=$1;
  216.         $2->n_x.v_subs[1]=$3;
  217.         $$ = $2; }
  218.     | exp '%' exp {
  219.         $2->n_x.v_subs[0]=$1;
  220.         $2->n_x.v_subs[1]=$3;
  221.         $$ = $2; }
  222.     | exp '^' exp {
  223.         $2->n_x.v_subs[0]=$1;
  224.         $2->n_x.v_subs[1]=$3;
  225.         $$ = $2; }
  226.     | '-' exp %prec NEG {
  227.         if($2->comp_value==CONST_FLT) {
  228.             $2->n_x.v_float= -($2->n_x.v_float);
  229.             /* free($1); */
  230.             $$=$2;
  231.         } else if($2->comp_value==CONST_INT) {
  232.             $2->n_x.v_int= -($2->n_x.v_int);
  233.             /* free($1); */
  234.             $$=$2;
  235.         } else {
  236.             $1->comp_value = NEGATE;
  237.             $1->n_x.v_subs[0]=$2;
  238.             $1->n_x.v_subs[1]=(struct node *)0;
  239.             $$ = $1;
  240.         } }
  241.     | '!' exp {
  242.         $1->n_x.v_subs[0]=$2;
  243.         $1->n_x.v_subs[1]=(struct node *)0;
  244.         $$ = $1; }
  245.     | '(' exp ')'
  246.         { $$ = $2; }
  247.     | '(' exp error {
  248.         if(!parse_error)
  249.             parse_error=NO_CLOSE;
  250.         }
  251.     /* | exp ')' error {
  252.         if(!parse_error)
  253.             parse_error=NO_OPEN;
  254.         } */
  255.     | '(' error {
  256.         if(!parse_error)
  257.             parse_error=NO_CLOSE;
  258.         }
  259.     ;
  260.  
  261.  
  262. exp_list: exp
  263.          { $$ = make_list($1, 0); }
  264.     | exp_list ',' exp
  265.         { $$ = make_list($3, $1); }
  266.     ;
  267.  
  268. range_exp: L_RANGE
  269.     | exp
  270.     ;
  271.  
  272. range_exp_list: range_exp
  273.         { $$=make_list($1, 0); }
  274.     |   range_exp_list ',' range_exp
  275.         { $$=make_list($3,$1); }
  276.     ;
  277.  
  278. cell:    L_CELL
  279.         { $$=$1; }
  280.     | L_VAR
  281.     ;
  282. %%
  283.  
  284. void
  285. yyerror FUN1(char *, s)
  286. {
  287.     if(!parse_error)
  288.         parse_error=PARSE_ERR;
  289. }
  290.  
  291. YYSTYPE
  292. make_list FUN2(YYSTYPE, car, YYSTYPE, cdr)
  293. {
  294.     YYSTYPE ret;
  295.  
  296.     ret=(YYSTYPE)obstack_alloc(&tmp_mem,sizeof(*ret));
  297.     ret->comp_value = 0;
  298.     ret->n_x.v_subs[0]=car;
  299.     ret->n_x.v_subs[1]=cdr;
  300.     return ret;
  301. }
  302.  
  303. #define ERROR -1
  304.  
  305. extern struct node *yylval;
  306.  
  307. #ifdef __STDC__
  308. unsigned char parse_cell_or_range (char **,struct rng *);
  309. int str_to_col (char ** str);
  310. #else
  311. unsigned char parse_cell_or_range ();
  312. #endif
  313.  
  314. int
  315. yylex FUN0()
  316. {
  317.     int ch;
  318.     struct node *new;
  319.     int isflt;
  320.     char *begin;
  321.     char *tmp_str;
  322.     unsigned char byte_value;
  323.     int n;
  324.  
  325.     /* unsigned char *ptr; */
  326.     int nn;
  327.     struct function *fp;
  328.     int tmp_ch;
  329.  
  330. #ifdef TEST
  331.     if(!instr)
  332.         return ERROR;
  333. #endif
  334.     while(isspace(*instr))
  335.         instr++;
  336.     ch = *instr++;
  337.     if(ch=='(' || ch==',' || ch==')')
  338.         return ch;
  339.  
  340.     new=(struct node *)obstack_alloc(&tmp_mem,sizeof(struct node));
  341.     new->add_byte=0;
  342.     new->sub_value=0;
  343.     switch(ch) {
  344.     case 0:
  345.         return 0;
  346.  
  347.     case '0': case '1': case '2': case '3': case '4': case '5': case '6':
  348.     case '7': case '8': case '9': case '.':
  349.         isflt = (ch=='.');
  350.  
  351.         begin=instr-1;
  352.         tmp_str=instr;
  353.  
  354.         while(isdigit(*tmp_str) || (!isflt && *tmp_str=='.' && ++isflt))
  355.             tmp_str++;
  356.         if(*tmp_str=='e' || *tmp_str=='E') {
  357.             isflt=1;
  358.             tmp_str++;
  359.             if(*tmp_str=='-' || *tmp_str=='+')
  360.                 tmp_str++;
  361.             while(isdigit(*tmp_str))
  362.                 tmp_str++;
  363.         }
  364.         if(isflt) {
  365.             new->n_x.v_float=astof((char **)(&begin));
  366.             byte_value=CONST_FLT;
  367.         } else {
  368.             new->n_x.v_int=astol((char **)(&begin));
  369.             if(begin!=tmp_str) {
  370.                 begin=instr-1;
  371.                 new->n_x.v_float=astof((char **)(&begin));
  372.                 byte_value=CONST_FLT;
  373.             } else
  374.                 byte_value=CONST_INT;
  375.         }
  376.         ch=L_CONST;
  377.         instr=begin;
  378.         break;
  379.  
  380.     case '"':
  381.         begin=instr;
  382.         while(*instr && *instr!='"') {
  383.             if(*instr=='\\' && instr[1])
  384.                 instr++;
  385.             instr++;
  386.         }
  387.         if(!*instr) {
  388.             parse_error=NO_QUOTE;
  389.             return ERROR;
  390.         }
  391.         tmp_str=new->n_x.v_string=(char *)ck_malloc(1+instr-begin);
  392.         while(begin!=instr) {
  393.             unsigned char n;
  394.  
  395.             if(*begin=='\\') {
  396.                 begin++;
  397.                 if(begin[0]>='0' && begin[0]<='7') {
  398.                     if(begin[1]>='0' && begin[1]<='7') {
  399.                         if(begin[2]>='0' && begin[2]<='7') {
  400.                             n=(begin[2]-'0') + (010 * (begin[1]-'0')) + ( 0100 * (begin[0]-'0'));
  401.                             begin+=3;
  402.                         } else {
  403.                             n=(begin[1]-'0') + (010 * (begin[0]-'0'));
  404.                             begin+=2;
  405.                         }
  406.                     } else {
  407.                         n=begin[0]-'0';
  408.                         begin++;
  409.                     }
  410.                 } else
  411.                     n= *begin++;
  412.                 *tmp_str++= n;
  413.             } else
  414.                 *tmp_str++= *begin++;
  415.         }
  416.         *tmp_str='\0';
  417.         instr++;
  418.         byte_value=CONST_STR;
  419.         ch=L_CONST;
  420.         break;
  421.  
  422.     case '+':    case '-':
  423.  
  424.     case '*':    case '/':    case '%':    case '&':
  425.     /* case '|': */    case '^':    case '=':
  426.  
  427.     case '?':
  428.     {
  429.         unsigned char *ptr;
  430.  
  431.         for(ptr= fnin;*ptr;ptr++)
  432.             if(the_funs[*ptr].fn_str[0]==ch)
  433.                 break;
  434. #ifdef TEST
  435.         if(!*ptr)
  436.             panic("Can't find fnin[] entry for '%c'",ch);
  437. #endif
  438.         byte_value= *ptr;
  439.     }
  440.         break;
  441.  
  442.     case ':':
  443.         byte_value=IF;
  444.         break;
  445.  
  446.     case '!':
  447.     case '<':
  448.     case '>':
  449.         if(*instr!='=') {
  450.             byte_value = (ch=='<') ? LESS : (ch=='>') ? GREATER : NOT;
  451.             break;
  452.         }
  453.         instr++;
  454.         byte_value = (ch=='<') ? LESSEQ : (ch=='>') ? GREATEQ : NOTEQUAL;
  455.         ch = (ch=='<') ? LE : (ch=='>') ? GE : NE;
  456.         break;
  457.  
  458.     case '\'':
  459.     case ';':
  460.     case '[':
  461.     case '\\':
  462.     case ']':
  463.     case '`':
  464.     case '{':
  465.     case '}':
  466.     case '~':
  467.     bad_chr:
  468.         parse_error=BAD_CHAR;
  469.         return ERROR;
  470.  
  471.     case '#':
  472.         begin=instr-1;
  473.         while(*instr && (isalnum(*instr) || *instr=='_'))
  474.             instr++;
  475.         ch= *instr;
  476.         *instr=0;
  477.         if(!stricmp(begin,tname))
  478.             byte_value=F_TRUE;
  479.         else if(!stricmp(begin,fname))
  480.             byte_value=F_FALSE;
  481.         else if(!stricmp(begin,iname) && (begin[4]==0 || !stricmp(begin+4,"inity")))
  482.             byte_value=CONST_INF;
  483.         else if(!stricmp(begin,mname) ||
  484.             !stricmp(begin,"#ninf"))
  485.             byte_value=CONST_NINF;
  486.         else if(!stricmp(begin,nname) ||
  487.             !stricmp(begin,"#nan"))
  488.             byte_value=CONST_NAN;
  489.         else {
  490.             for(n=1;n<=ERR_MAX;n++)
  491.                 if(!stricmp(begin,ename[n]))
  492.                     break;
  493.             if(n>ERR_MAX)
  494.                 n=BAD_CHAR;
  495.             new->n_x.v_int=n;
  496.             byte_value=CONST_ERR;
  497.         }
  498.         *instr=ch;
  499.         ch=L_CONST;
  500.         break;
  501.  
  502.     default:
  503.         if(!a0 && (ch=='@' || ch=='$'))
  504.            goto bad_chr;
  505.  
  506.         if(a0 && ch=='@') {
  507.             begin=instr;
  508.             while(*instr && (isalpha(*instr) || isdigit(*instr) || *instr=='_'))
  509.                 instr++;
  510.             n=instr-begin;
  511.         } else {
  512.             begin=instr-1;
  513.             byte_value=parse_cell_or_range(&begin,&(new->n_x.v_rng));
  514.             if(byte_value) {
  515.                 if((byte_value& ~0x3)==R_CELL)
  516.                     ch=L_CELL;
  517.                 else
  518.                     ch=L_RANGE;
  519.                 instr=begin;
  520.                 break;
  521.             }
  522.  
  523.             while(*instr && (isalpha(*instr) || isdigit(*instr) || *instr=='_'))
  524.                 instr++;
  525.  
  526.             n=instr-begin;
  527.             while(isspace(*instr))
  528.                 instr++;
  529.  
  530.             if(*instr!='(') {
  531.                 ch=L_VAR;
  532.                 byte_value=VAR;
  533.                 new->n_x.v_var=find_or_make_var(begin,n);
  534.                 break;
  535.             }
  536.         }
  537.         tmp_ch=begin[n];
  538.         begin[n]='\0';
  539.         fp=hash_find(parse_hash,begin);
  540.         begin[n]=tmp_ch;
  541.         byte_value= ERROR;
  542.         if(!fp) {
  543.             parse_error=BAD_FUNC;
  544.             return ERROR;
  545.         }
  546.  
  547.         if(fp>=the_funs && fp<=&the_funs[USR1])
  548.             byte_value=fp-the_funs;
  549.         else {
  550.             for(nn=0;nn<n_usr_funs;nn++) {
  551.                 if(fp>=&usr_funs[nn][0] && fp<=&usr_funs[nn][usr_n_funs[nn]]) {
  552.                     byte_value=USR1+nn;
  553.                     new->sub_value=fp-&usr_funs[nn][0];
  554.                     break;
  555.                 }
  556.             }
  557. #ifdef TEST
  558.             if(nn==n_usr_funs) {
  559.                 io_error_msg("Couln't turn fp into a ##");
  560.                 parse_error=BAD_FUNC;
  561.                 return ERROR;
  562.             }
  563. #endif
  564.         }
  565.  
  566.         if(fp->fn_argn&X_J)
  567.             ch= byte_value==F_IF ? L_FN3 : L_FN2;
  568.         else if(fp->fn_argt[0]=='R' || fp->fn_argt[0]=='E')
  569.             ch=L_FN1R-1+fp->fn_argn-X_A0;
  570.         else
  571.             ch=L_FN0 + fp->fn_argn-X_A0;
  572.  
  573.         break;
  574.     }
  575.     /* new->node_type=ch; */
  576.     new->comp_value=byte_value;
  577.     yylval=new;
  578.     return ch;
  579. }
  580.  
  581. /* Return value is
  582.     0 if it doesn't look like a cell or a range,
  583.     R_CELL if it is a cell (ptr now points past the characters, lr and lc hold the row and col of the cell)
  584.     RANGE if it is a range (ptr points past the chars)
  585.  */
  586. unsigned char
  587. parse_cell_or_range FUN2(char **,ptr, struct rng *,retp)
  588. {
  589.     if(a0) {
  590.         unsigned tmpc,tmpr;
  591.         char *p;
  592.         int abz = ROWREL|COLREL;
  593.  
  594.         p= *ptr;
  595.         tmpc=0;
  596.         if(*p=='$') {
  597.             abz-=COLREL;
  598.             p++;
  599.         }
  600.         if(!isalpha(*p))
  601.             return 0;
  602.         tmpc=str_to_col(&p);
  603.         if(tmpc<MIN_COL || tmpc>MAX_COL)
  604.             return 0;
  605.         if(*p=='$') {
  606.             abz-=ROWREL;
  607.             p++;
  608.         }
  609.         if(!isdigit(*p))
  610.             return 0;
  611.         for(tmpr=0;isdigit(*p);p++)
  612.             tmpr=tmpr*10 + *p - '0';
  613.  
  614.         if(tmpr<MIN_ROW || tmpr>MAX_ROW)
  615.             return 0;
  616.  
  617.         if(*p==':' || *p=='.') {
  618.             unsigned tmpc1,tmpr1;
  619.  
  620.             abz = ((abz&COLREL) ? LCREL : 0)|((abz&ROWREL) ? LRREL : 0)|HRREL|HCREL;
  621.             p++;
  622.             if(*p=='$') {
  623.                 abz-=HCREL;
  624.                 p++;
  625.             }
  626.             if(!isalpha(*p))
  627.                 return 0;
  628.             tmpc1=str_to_col(&p);
  629.             if(tmpc1<MIN_COL || tmpc1>MAX_COL)
  630.                 return 0;
  631.             if(*p=='$') {
  632.                 abz-=HRREL;
  633.                 p++;
  634.             }
  635.             if(!isdigit(*p))
  636.                 return 0;
  637.             for(tmpr1=0;isdigit(*p);p++)
  638.                 tmpr1=tmpr1*10 + *p - '0';
  639.             if(tmpr1<MIN_ROW || tmpr1>MAX_ROW)
  640.                 return 0;
  641.  
  642.             if(tmpr<tmpr1) {
  643.                 retp->lr=tmpr;
  644.                 retp->hr=tmpr1;
  645.             } else {
  646.                 retp->lr=tmpr1;
  647.                 retp->hr=tmpr;
  648.             }
  649.             if(tmpc<tmpc1) {
  650.                 retp->lc=tmpc;
  651.                 retp->hc=tmpc1;
  652.             } else {
  653.                 retp->lc=tmpc1;
  654.                 retp->hc=tmpc;
  655.             }
  656.             *ptr= p;
  657.             return RANGE | abz;
  658.         }
  659.         retp->lr = retp->hr = tmpr;
  660.         retp->lc = retp->hc = tmpc;
  661.         *ptr=p;
  662.         return R_CELL | abz;
  663.     } else {
  664.         char *p;
  665.         unsigned char retr;
  666.         unsigned char retc;
  667.         int ended;
  668.         long num;
  669.         CELLREF tmp;
  670.  
  671. #define CK_ABS_R(x)    ((x)<MIN_ROW || (x)>MAX_ROW)
  672.  
  673. #define CK_REL_R(x)    (((x)>0 && MAX_ROW-(x)<cur_row) \
  674.              || ((x)<0 && MIN_ROW-(x)>cur_row)) 
  675.  
  676. #define CK_ABS_C(x)    ((x)<MIN_COL || (x)>MAX_COL)
  677.  
  678. #define CK_REL_C(x)    (((x)>0 && MAX_COL-(x)<cur_col)    \
  679.              || ((x)<0 && MIN_COL-(x)>cur_col))
  680.  
  681. #define MAYBEREL(p) (*(p)=='[' && (isdigit((p)[1]) || (((p)[1]=='+' || (p)[1]=='-') && isdigit((p)[2]))))
  682.  
  683.         p= *ptr;
  684.         retr=0;
  685.         retc=0;
  686.         ended=0;
  687.         while(ended==0) {
  688.             switch(*p) {
  689.             case 'r':
  690.             case 'R':
  691.                 if(retr) {
  692.                     ended++;
  693.                     break;
  694.                 }
  695.                 p++;
  696.                 retr=R_CELL;
  697.                 if(isdigit(*p)) {
  698.                     num=astol(&p);
  699.                     if (CK_ABS_R(num))
  700.                       return 0;
  701.                     retp->lr= retp->hr=num;
  702.                 } else if(MAYBEREL(p)) {
  703.                     p++;
  704.                     num=astol(&p);
  705.                     if (CK_REL_R(num))
  706.                       return 0;
  707.                     retp->lr= retp->hr=num+cur_row;
  708.                     retr|=ROWREL;
  709.                     if(*p==':') {
  710.                         retr=RANGE|LRREL|HRREL;
  711.                         p++;
  712.                         num=astol(&p);
  713.                         if (CK_REL_R(num))
  714.                           return 0;
  715.                         retp->hr=num+cur_row;
  716.                     }
  717.                     if(*p++!=']')
  718.                         return 0;
  719.                 } else if(retc || *p=='c' || *p=='C') {
  720.                     retr|=ROWREL;
  721.                     retp->lr= retp->hr=cur_row;
  722.                 } else
  723.                     return 0;
  724.                 if(*p==':' && retr!=(RANGE|LRREL|HRREL)) {
  725.                     retr= (retr&ROWREL) ? RANGE|LRREL : RANGE;
  726.                     p++;
  727.                     if(isdigit(*p)) {
  728.                         num=astol(&p);
  729.                         if (CK_ABS_R(num))
  730.                           return 0;
  731.                          retp->hr=num;
  732.                     } else if(MAYBEREL(p)) {
  733.                         p++;
  734.                         num=astol(&p);
  735.                         if (CK_REL_R(num))
  736.                           return 0;
  737.                         retp->hr=num+cur_row;
  738.                         retr|=HRREL;
  739.                         if(*p++!=']')
  740.                             return 0;
  741.                     } else
  742.                         return 0;
  743.                 }
  744.  
  745.                 if(retc)
  746.                     ended++;
  747.                 break;
  748.  
  749.             case 'c':
  750.             case 'C':
  751.                 if(retc) {
  752.                     ended++;
  753.                     break;
  754.                 }
  755.                 p++;
  756.                 retc=R_CELL;
  757.                 if(isdigit(*p)) {
  758.                     num=astol(&p);
  759.                     if (CK_ABS_C(num))
  760.                       return 0;
  761.                     retp->lc= retp->hc=num;
  762.                 } else if(MAYBEREL(p)) {
  763.                     p++;
  764.                     num=astol(&p);
  765.                     if (CK_REL_C(num))
  766.                       return 0;
  767.                     retp->lc= retp->hc=num+cur_col;
  768.                     retc|=COLREL;
  769.                     if(*p==':') {
  770.                         retc=RANGE|LCREL|HCREL;
  771.                         p++;
  772.                         num=astol(&p);
  773.                         if (CK_REL_C(num))
  774.                           return 0;
  775.                         retp->hc=num+cur_col;
  776.                     }
  777.                     if(*p++!=']')
  778.                         return 0;
  779.                 } else if(retr || *p=='r' || *p=='R') {
  780.                     retc|=COLREL;
  781.                     retp->lc= retp->hc=cur_col;
  782.                 } else
  783.                     return 0;
  784.                 if(*p==':' && retc!=(RANGE|LCREL|HCREL)) {
  785.                     retc= (retc&COLREL) ? RANGE|LCREL : RANGE;
  786.                     p++;
  787.                     if(isdigit(*p)) {
  788.                         num=astol(&p);
  789.                         if (CK_ABS_C(num))
  790.                           return 0;
  791.                          retp->hc=num;
  792.                     } else if(MAYBEREL(p)) {
  793.                         p++;
  794.                         num=astol(&p);
  795.                         if (CK_REL_C(num))
  796.                           return 0;
  797.                         retp->hc=num+cur_col;
  798.                         retc|=HCREL;
  799.                         if(*p++!=']')
  800.                             return 0;
  801.                     } else
  802.                         return 0;
  803.                 }
  804.  
  805.                 if(retr)
  806.                     ended++;
  807.                 break;
  808.             default:
  809.                 if(retr) {
  810.                     *ptr=p;
  811.                     retp->lc=MIN_COL;
  812.                     retp->hc=MAX_COL;
  813.                     if((retr|ROWREL)==(R_CELL|ROWREL))
  814.                         return (retr&ROWREL) ? (RANGE|LRREL|HRREL) : RANGE;
  815.                     else
  816.                         return retr;
  817.                 } else if(retc) {
  818.                     *ptr=p;
  819.                     retp->lr=MIN_ROW;
  820.                     retp->hr=MAX_COL;
  821.                     if((retc|COLREL)==(R_CELL|COLREL))
  822.                         return (retc&COLREL) ? (RANGE|LCREL|HCREL) : RANGE;
  823.                     else
  824.                         return retc;
  825.                 }
  826.                 return 0;
  827.             }
  828.         }
  829.         if(!retr || !retc)
  830.             return 0;
  831.         *ptr=p;
  832.         if(retp->lr>retp->hr)
  833.             tmp=retp->lr,retp->lr=retp->hr,retp->hr=tmp;
  834.         if(retp->lc>retp->hc)
  835.             tmp=retp->lc,retp->lc=retp->hc,retp->hc=tmp;
  836.  
  837.         if((retr|ROWREL)==(R_CELL|ROWREL)) {
  838.             if((retc|COLREL)==(R_CELL|COLREL))
  839.                 return retr|retc;
  840.             return (retr&ROWREL) ? (retc|LRREL|HRREL) : retc;
  841.         }
  842.         if((retc|COLREL)==(R_CELL|COLREL))
  843.             return (retc&COLREL) ? (retr|LCREL|HCREL) : retr;
  844.         return retr|retc;
  845.     }
  846. }
  847.  
  848. int
  849. str_to_col FUN1(char **,str)
  850. {
  851.     int ret;
  852.     char c,cc,ccc;
  853. #if MAX_COL>702
  854.     char cccc;
  855. #endif
  856.  
  857.     ret=0;
  858.     c=str[0][0];
  859.     if(!isalpha((cc=str[0][1]))) {
  860.         (*str)++;
  861.         return MIN_COL + (isupper(c) ? c-'A' : c-'a');
  862.     }
  863.     if(!isalpha((ccc=str[0][2]))) {
  864.         (*str)+=2;
  865.         return MIN_COL+26 + (isupper(c) ? c-'A' : c-'a')*26 + (isupper(cc) ? cc-'A' : cc-'a');
  866.     }
  867. #if MAX_COL>702
  868.     if(!isalpha((cccc=str[0][3]))) {
  869.         (*str)+=3;
  870.         return MIN_COL+702 + (isupper(c) ? c-'A' : c-'a')*26*26 + (isupper(cc) ? cc-'A' : cc-'a')*26 + (isupper(ccc) ? ccc-'A' : ccc-'a');
  871.     }
  872.     if(!isalpha(str[0][4])) {
  873.         (*str)+=4;
  874.         return MIN_COL+18278 + (isupper(c) ? c-'A' : c-'a')*26*26*26 + (isupper(cc) ? cc-'A' : cc-'a')*26*26 + (isupper(ccc) ? ccc-'A' : ccc-'a')*26 + (isupper(cccc) ? cccc-'A' : cccc-'a');
  875.     }
  876. #endif
  877.     return 0;
  878. }
  879.